package com.lab303.powerproject.util;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

public class GenerateDemoData {

    public static Map<String,float[]> doGenerate(float tem1,float tem2,float hum1,float hum2,int flag,int peopleNumber){
        float[] temperatureArr ;
        float[] humidityArr;
        Random random = new Random();
        float[] meetingData = new float[24];
        float[] restaurantData = new float[24];
        int meetingTime = random.nextInt(6)+10;
        while (meetingTime==11|meetingTime<=12)
            meetingTime = random.nextInt(6)+10;
        meetingData[meetingTime] = 40+random.nextInt(20)*0.1f;
        meetingData[meetingTime-1] = 30+random.nextInt(20)*0.1f;
        meetingData[meetingTime+1] = 30+random.nextInt(20)*0.1f;
        restaurantData[13] = 55+random.nextInt(20)*0.1f;
        restaurantData[12] = 60+random.nextInt(20)*0.1f;
        restaurantData[11] = 50+random.nextInt(20)*0.1f;
        restaurantData[8] = 30+random.nextInt(20)*0.1f;
        restaurantData[7] = 70+random.nextInt(20)*0.1f;

        float[]  bangong= new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,5.0f,28.0f,29.0f,30.0f,29.0f,30.0f,28.0f,29.0f,30.0f,29.0f,30.0f,30.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
        float[] zhaoming = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,35.0f,40.0f,40.0f,40.0f,38.0f,38.0f,37.0f,39.0f,38.0f,36.0f,30.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
        float[] xiaofang = new float[]{8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f};
        float[] other = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,9.5f,8.0f,9.0f,8.5f,0.0f,0.0f,0.0f,0.0f,0.0f};

        if(tem1>=16&&tem2<26){
            for (int i = 0 ;i<24;i++){
                restaurantData[i] = 0f;
                meetingData[i] = 0f;
            }
        }

        if (tem1==-6&&tem2==5){
            temperatureArr = new float[]{-4.5f,-5.1f,-5.4f,-5.6f,-5.8f,-5.9f,-6.0f,-5.0f,-3.0f,-1.0f,0.0f,1.4f,2.6f,3.9f,5.0f,4.5f,3.5f,2.0f,0.3f,-1.4f,-2.8f,-3.4f,-3.9f,-4.2f};
            for (int i = 0  ; i<temperatureArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                temperatureArr[i] = temperatureArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if (tem1==-14&&tem2==-3){
            temperatureArr = new float[]{-10.5f,-11.2f,-12.1f,-12.9f,-13.6f,-13.9f,-14.0f,-12.1f,-10.9f,-8.9f,-7.8f,-6.5f,-5.1f,-3.9f,-3.0f,-3.3f,-4.1f,-4.9f,-5.5f,-6.2f,-7.0f,-7.8f,-8.5f,-9.1f};
            for (int i = 0  ; i<temperatureArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                temperatureArr[i] = temperatureArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if (tem1==24&&tem2==37){
            temperatureArr = new float[]{26.7f,25.8f,24.7f,24.1f,24.0f,24.6f,25.3f,26.3f,27.6f,29.1f,30.8f,32.5f,34.3f,35.9f,37.0f,36.5f,35.5f,34.3f,32.9f,31.7f,30.9f,30.3f,29.8f,29.2f};
            for (int i = 0  ; i<temperatureArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                temperatureArr[i] = temperatureArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if (tem1==25&&tem2==36){
            temperatureArr = new float[]{25.2f,25.1f,25.0f,25.0f,25.0f,25.1f,25.3f,25.7f,26.3f,27.1f,28.2f,29.6f,31.5f,33.8f,36.0f,35.2f,34.3f,33.0f,31.6f,30.3f,29.2f,28.4f,27.7f,27.2f};
            for (int i = 0  ; i<temperatureArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                temperatureArr[i] = temperatureArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if (tem1==23&&tem2==31){
            temperatureArr = new float[]{23.9f,23.8f,23.6f,23.3f,23.0f,23.2f,24.1f,24.4f,24.9f,25.7f,26.8f,27.5f,28.6f,29.9f,31.0f,30.5f,30.1f,29.4f,29.0f,28.5f,27.7f,27.0f,26.3f,25.8f};
            for (int i = 0  ; i<temperatureArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                temperatureArr[i] = temperatureArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else {
            temperatureArr = GenerateDemo.generateTemperature(tem1,tem2);
        }
        if (hum1==40&&hum2==70){
            humidityArr = new float[]{66.0f,65.0f,64.9f,65.5f,70.0f,66.5f,66.0f,64.0f,60.4f,55.5f,50.2f,45.3f,41.3f,40.9f,40.0f,40.9f,44.3f,48.6f,53.4f,58.0f,62.1f,65.8f,69.1f,72.1f};
            for (int i = 0  ; i<humidityArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                humidityArr[i] = humidityArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if (hum1==60&&hum2==100){
            humidityArr = new float[]{96.1f,97.0f,98.1f,99.3f,99.8f,99.8f,98.2f,95.0f,89.5f,82.7f,75.5f,68.7f,63.2f,62.8f,60.0f,61.9f,65.8f,70.8f,76.1f,81.0f,85.0f,88.0f,90.2f,91.9f};
            for (int i = 0  ; i<humidityArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                humidityArr[i] = humidityArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else if(hum1==50&&hum2==90){
            humidityArr = new float[]{88.5f,89.1f,89.8f,90.0f,90.0f,87.7f,73.8f,70.0f,66.7f,63.8f,61.2f,58.7f,56.4f,54.0f,50.0f,49.3f,47.4f,46.2f,46.0f,47.0f,49.4f,53.0f,57.3f,76.0f};
            for (int i = 0  ; i<humidityArr.length;i++){
                int f = random.nextInt(2)==0?-1:1;
                humidityArr[i] = humidityArr[i]+f*(random.nextInt(10)+1)*0.1f;
            }
        }else {
            humidityArr = GenerateDemo.generateHumidity(hum1,hum2);
        }
        float[] nuandian = new float[24];
        float[] xuneng = new float[24];
        float[] dimaiguan = new float[24];
        float[] zhujizhigong = new float[24];
        float[] zhujixuleng = new float[24];
        float[] guangre;
        float[] dianliang = new float[24];
        float[] xinfeng = new float[24];
        float[] xuqiu = new float[24];
        Map<String,float[]> data = new LinkedHashMap<>();
        for (int i = 0 ; i< 24;i++){
            if (i>=5&&i<=6)
                nuandian[i] = 90;
            else
                nuandian[i] = 0;
        }
        if (flag == 0){     //冬季
            guangre = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,23.5f,58.7f,101.5f,119.8f,129.7f,116.5f,98.6f,30.1f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
            for (int i = 0 ; i< 24 ; i++){    //计算冬季需求热量
                if (i <= 6 ||i>=18)
                    xuqiu[i] = 0;
                else
                    if(temperatureArr[i]>=16){
                        xuqiu[i] = 0;
                    }else {
                        xuqiu[i] = (20 - temperatureArr[i]) / 28 * 560;
                    }
            }
            for (int i = 0 ; i<24;i++){
                xuqiu[i] = xuqiu[i]+meetingData[i]+restaurantData[i];
            }
            /*for (int i = 0 ; i< 24;i++){    //考虑人数因素，默认400人，上下波动，但是冬季目前不考虑
                xuqiu[i] = (float) (xuqiu[i]*(1+0.0002*(peopleNumber-400)));
            }*/
            for (int i = 0 ; i < 24 ;i++){   //计算冬季水蓄能放热量
                if (i <=6|i>=18){
                    xuneng[i] = 0;
                }else {
                    if (xuqiu[i]-guangre[i]>=0){
                        xuneng[i] = xuqiu[i]-guangre[i];
                    }else {
                        xuneng[i] = 0;
                    }
                }
            }
            float xuSum = xuneng[7];
            for (int i = 8 ;i <=18 ;i++){     //判断水蓄罐放热两是否达到最大储热量
                if (xuSum+xuneng[i]<4876.2){
                    xuSum+=xuneng[i];
                    continue;
                }else {
                    xuneng[i] = (float) (4876.2-xuSum);
                }

            }
            for (int i = 0 ;i<24;i++){   //判断主机是否需要只供热量
                if (i<=10|i>=18){
                    zhujizhigong[i]=0;
                }else {
                    if (xuqiu[i]-guangre[i]-xuneng[i]>=0){
                        zhujizhigong[i]=xuqiu[i]-guangre[i]-xuneng[i];
                    }else
                        zhujizhigong[i] = 0;
                }
            }
            float sum = 0;
            for (float v:xuneng){   //通过水蓄罐与主机蓄热的关系推出主机需要蓄多少能，同时单位小时蓄热上限为640
                sum+=v;
            }
            sum*=1.05;
            if (sum>640){
                for (int i = 6 ; i>= 0 ; i--){
                    if (sum>640) {
                        zhujixuleng[i] =  640;
                        sum -= 640;
                    }else {
                        zhujixuleng[i] = sum;
                        sum = 0;
                        break;
                    }
                }
                if(sum>640)
                    zhujixuleng[23] = 640;
                else if (sum>0)
                    zhujixuleng[23] = sum;
            }else {
                zhujixuleng[6] = sum;
            }
            for (int i = 0 ; i < 24 ; i++ ){    //根据公式通过能量计算出电量
                dianliang[i] = zhujixuleng[i]/3+zhujizhigong[i]/3+xuneng[i]/10+guangre[i]/10;
            }

        }else {
            guangre = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,23.5f,58.7f,101.5f,5.3f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
            for (int i = 0 ; i<24 ;i++){    //根据公式计算需求冷量
                if (i<=6||i>=18) {
                    xuqiu[i] = 0;
                }else {
                    if(temperatureArr[i]>=26) {
                        if (humidityArr[i] < 80) {
                            xuqiu[i] = (temperatureArr[i] - 24) / 13 * 700;
                        } else {
                            xuqiu[i] = (temperatureArr[i] - 24) / 10 * 700 + (humidityArr[i] - 40);
                        }
                    }else
                        xuqiu[i] = 0;
                }
            }
            for (int i = 0 ; i<24;i++){
                xuqiu[i] = xuqiu[i]+meetingData[i]+restaurantData[i];
            }
            for (int i = 0 ; i< 24;i++){    //考虑人数因素
                xuqiu[i] = (float) (xuqiu[i]*(1+0.0005*(peopleNumber-400)));
            }
            for (int i = 0 ; i<24;i++){   //计算免费冷需求量，上限为200
                if (i<=6||i>=18) {
                    if(temperatureArr[i]>=24) {
                        if ((temperatureArr[i] - 24) * 20 <= 200)
                            dimaiguan[i] = (temperatureArr[i] - 24) * 20;
                        else
                            dimaiguan[i]  = 200;
                    }else {
                        dimaiguan[i] = 0;
                    }
                }else {
                    if (xuqiu[i]<=200){
                        dimaiguan[i] = xuqiu[i];
                    }else {
                        dimaiguan[i] = 200;
                    }
                }
            }
            for (int i = 0 ; i< 24 ;i++){   //计算新风机组供冷量，上限为230
                if (i<=6||i>=18){
                    xinfeng[i] = 0;
                }else {
                    if (humidityArr[i]<40|temperatureArr[i]<24){
                        xinfeng[i] = 0;
                    }else {
                        if (((temperatureArr[i]-24)*20+humidityArr[i]-40)>(xuqiu[i]-dimaiguan[i])){
                            if (xuqiu[i]-dimaiguan[i]>=0) {
                                if (xuqiu[i] - dimaiguan[i] <= 230)
                                    xinfeng[i]= xuqiu[i] - dimaiguan[i];
                                else
                                    xinfeng[i] = 230;
                            }else
                                xinfeng[i] = 0;
                        }else {
                            if((temperatureArr[i]-24)*20+humidityArr[i]-40<=230)
                                xinfeng[i] = (temperatureArr[i]-24)*20+humidityArr[i]-40;
                            else
                                xinfeng[i] = 230;
                        }
                    }
                }
            }

            for (int i = 0 ; i<24 ;i++){   //计算水蓄罐蓄冷量
                if (i<=6||i>=18){
                    xuneng[i] = 0;
                }else {
                    float v = xuqiu[i] - dimaiguan[i] - xinfeng[i];
                    if (v>=0) {
                            xuneng[i] = v;
                    }else {
                        xuneng[i] =0;
                    }
                }
            }
            float xuSum = xuneng[7];    //蓄能上限2651.4
            for (int i = 8 ;i <=18 ;i++){
                if (xuSum+xuneng[i]<2651.4){
                    xuSum+=xuneng[i];
                    continue;
                }else {
                    xuneng[i] = (float) (2651.4-xuSum);
                }

            }

            for (int i = 0 ; i<24;i++){   //计算主机只供供冷量
                if (i<=10|i>=18){
                    zhujizhigong[i] = 0;
                }else {
                    if(xuqiu[i]-dimaiguan[i]-xinfeng[i]-xuneng[i]>=0)
                        zhujizhigong[i] = xuqiu[i]-dimaiguan[i]-xinfeng[i]-xuneng[i];
                    else
                        zhujizhigong[i]=0;
                }
            }

            float sum = 0;
            for (float v:xuneng){
                sum+=v;
            }
            sum*=1.05;
            if (sum>600){
                for (int i = 6 ; i>= 0 ; i--){   //蓄冷上限600
                    if (sum>600) {
                        zhujixuleng[i] = 600;
                        sum -= 600;
                    }else if(sum>=0){
                        zhujixuleng[i] = sum;
                        sum = 0;
                        break;
                    }
                }
                if(sum>=600)
                    zhujixuleng[23] = 600;
                else if (sum>0)
                    zhujixuleng[23] = sum;
            }else {
                zhujixuleng[6] = sum;
            }
            for (int i = 0 ; i < 24 ; i++ ){   //根据公式计算电量
                dianliang[i] = dimaiguan[i]/20+ zhujizhigong[i]/3+zhujixuleng[i]/3+ xuneng[i]/10+ xinfeng[i]/3;
            }
        }
        for (int i = 0 ; i<24;i++){
            xuqiu[i] = xuqiu[i]-meetingData[i]-restaurantData[i];
        }
        for (int i= 0 ; i<24;i++){
            nuandian[i] = new BigDecimal(nuandian[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            xuneng[i] = new BigDecimal(xuneng[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            dimaiguan[i] = new BigDecimal(dimaiguan[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            zhujizhigong[i] = new BigDecimal(zhujizhigong[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            zhujixuleng[i] = new BigDecimal(zhujixuleng[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            guangre[i] = new BigDecimal(guangre[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            dianliang[i] = new BigDecimal(dianliang[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            xinfeng[i] = new BigDecimal(xinfeng[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
            xuqiu[i] = new BigDecimal(xuqiu[i]).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
        }
        data.put("temperature",temperatureArr);
        data.put("humidity",humidityArr);
        data.put("nuandian",nuandian);
        data.put("xuneng",xuneng);
        data.put("dimaiguan",dimaiguan);
        data.put("zhujizhigong",zhujizhigong);
        data.put("zhujixuleng",zhujixuleng);
        data.put("guangre",guangre);
        data.put("dianliang",dianliang);
        data.put("xinfeng",xinfeng);
        data.put("xuqiu",xuqiu);
        data.put("bangong",bangong);
        data.put("zhaoming",zhaoming);
        data.put("xiaofang",xiaofang);
        data.put("other",other);
        data.put("meetingData",meetingData);
        data.put("restaurantData",restaurantData);
        return data;
    }

    public static void main(String[] args) throws IOException {
        float tem1 =16;
        float tem2 = 25;
        float hum1 = 40;
        float hum2 = 60;
        int flag = 1;
        int peopleNum = 400;
        Map<String, float[]> stringMap = GenerateDemoData.doGenerate(tem1, tem2, hum1, hum2, flag,peopleNum);
        System.out.println(stringMap.size());
    }

}
